# config_dialog.py
from PySide2.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QFormLayout,
                               QLabel, QLineEdit, QPushButton, QDialogButtonBox, QCheckBox)
from PySide2.QtCore import Qt
import os
from config_manager import ConfigManager


class ConfigDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("系统配置")
        self.setMinimumWidth(640)

        # 加载配置
        self.config_manager = ConfigManager()

        # 创建表单布局
        layout = QFormLayout(self)

        # Chrome可执行文件路径
        self.chrome_path_edit = QLineEdit()
        self.chrome_path_edit.setText(self.config_manager.get_chrome_path().replace("/", os.sep))
        chrome_browse_btn = QPushButton("浏览...")
        chrome_browse_btn.clicked.connect(self.browse_chrome_exe)

        chrome_layout = QHBoxLayout()
        chrome_layout.addWidget(self.chrome_path_edit)
        chrome_layout.addWidget(chrome_browse_btn)

        # Chrome驱动路径
        self.driver_path_edit = QLineEdit()
        self.driver_path_edit.setText(self.config_manager.get_driver_path().replace("/", os.sep))
        driver_browse_btn = QPushButton("浏览...")
        driver_browse_btn.clicked.connect(self.browse_driver_exe)

        driver_layout = QHBoxLayout()
        driver_layout.addWidget(self.driver_path_edit)
        driver_layout.addWidget(driver_browse_btn)

        # 堡垒机信息文件路径
        self.login_path_edit = QLineEdit()
        self.login_path_edit.setText(self.config_manager.get_login_path())
        login_browse_btn = QPushButton("浏览...")
        login_browse_btn.clicked.connect(self.browse_login_xlsx)

        bastion_layout = QHBoxLayout()
        bastion_layout.addWidget(self.login_path_edit)
        bastion_layout.addWidget(login_browse_btn)

        # 添加堡垒机表格列名配置
        self.bastion_columns_edit = QLineEdit()
        self.bastion_columns_edit.setText(", ".join(self.config_manager.get_bastion_columns()))
        self.bastion_columns_edit.setPlaceholderText("请输入堡垒机表格列名，用逗号分隔")

        # 添加窗口大小配置
        self.width_edit = QLineEdit()
        self.height_edit = QLineEdit()
        size_layout = QHBoxLayout()
        size_layout.addWidget(QLabel("宽:"))
        size_layout.addWidget(self.width_edit)
        size_layout.addWidget(QLabel("高:"))
        size_layout.addWidget(self.height_edit)

        # 加载当前窗口大小配置
        config = ConfigManager()
        width, height = config.get_window_size()
        self.width_edit.setText(str(width))
        self.height_edit.setText(str(height))

        # 添加窗口置顶复选框
        top_layout = QHBoxLayout()
        self.top_checkbox = QCheckBox("窗口置顶")

        # 添加默认提交复选框
        submit_layout = QHBoxLayout()
        self.submit_checkbox = QCheckBox("默认提交")
        self.submit_checkbox.setToolTip("设置自动登录模块是否默认自动提交")
        self.submit_checkbox.setChecked(self.config_manager.get_auto_submit())

        # 添加日志启用复选框
        log_layout = QHBoxLayout()
        self.log_checkbox = QCheckBox("启用日志")
        self.log_checkbox.setToolTip("启用或禁用程序日志功能")
        self.log_checkbox.setChecked(self.config_manager.get_log_enabled())
        log_layout.addWidget(self.log_checkbox)

        # 添加到表单
        layout.addRow("Chrome可执行文件路径:", chrome_layout)
        layout.addRow("Chrome驱动文件路径:", driver_layout)
        layout.addRow("堡垒机信息文件路径:", bastion_layout)
        layout.addRow("窗口大小:", size_layout)
        top_layout.addWidget(self.top_checkbox)
        layout.addRow("窗口置顶:", top_layout)
        submit_layout.addWidget(self.submit_checkbox)
        layout.addRow("默认提交:", submit_layout)
        # 在布局中添加日志启用选项
        layout.addRow("日志功能:", log_layout)
        layout.addRow("堡垒机表格列名:", self.bastion_columns_edit)

        # 对话框按钮
        buttons = QDialogButtonBox()
        save_button = QPushButton("保存")
        cancel_button = QPushButton("取消")
        buttons.addButton(save_button, QDialogButtonBox.AcceptRole)
        buttons.addButton(cancel_button, QDialogButtonBox.RejectRole)
        layout.addRow(buttons)

        # 加载当前配置
        config = ConfigManager()
        self.top_checkbox.setChecked(self.config_manager.get_window_on_top())
        save_button.clicked.connect(self.accept)
        cancel_button.clicked.connect(self.reject)


    def browse_chrome_exe(self):
        from PySide2.QtWidgets import QFileDialog
        file_filter = "可执行文件 (*.exe);;所有文件 (*.*)" if os.name == 'nt' else "所有文件 (*)"
        path, _ = QFileDialog.getOpenFileName(
            self, "选择Chrome可执行文件", self.chrome_path_edit.text(), file_filter
        )
        if path:
            self.chrome_path_edit.setText(path.replace("/", os.sep))

    def browse_driver_exe(self):
        from PySide2.QtWidgets import QFileDialog
        file_filter = "可执行文件 (*.exe);;所有文件 (*.*)" if os.name == 'nt' else "所有文件 (*)"
        path, _ = QFileDialog.getOpenFileName(
            self, "选择Chrome驱动文件", self.driver_path_edit.text(), file_filter
        )
        if path:
            self.driver_path_edit.setText(path.replace("/", os.sep))

    def browse_login_xlsx(self):
        from PySide2.QtWidgets import QFileDialog
        file_filter = "堡垒机信息 (*.xlsx);;所有文件 (*.*)" if os.name == 'nt' else "所有文件 (*)"
        path, _ = QFileDialog.getOpenFileName(
            self, "选择堡垒机信息文件", self.login_path_edit.text(), file_filter
        )
        if path:
            self.login_path_edit.setText(path.replace("/", os.sep))

    def accept(self):
        # 保存设置
        self.config_manager.set_log_enabled(self.log_checkbox.isChecked())
        self.config_manager.set_chrome_path(self.chrome_path_edit.text())
        self.config_manager.set_driver_path(self.driver_path_edit.text())
        self.config_manager.set_login_path(self.login_path_edit.text())
        self.config_manager.set_window_on_top(self.top_checkbox.isChecked())
        self.config_manager.set_auto_submit(self.submit_checkbox.isChecked())
        self.config_manager.set_window_size(
            self.width_edit.text(),
            self.height_edit.text()
        )
        # 保存表格列名配置
        self.config_manager.set_bastion_columns([col.strip() for col in self.bastion_columns_edit.text().split(',') if col.strip()])

        super().accept()

    def get_config(self):
        return {
            "chrome_path": self.chrome_path_edit.text(),
            "driver_path": self.driver_path_edit.text(),
            "login_path": self.login_path_edit.text(),
            "window_on_top": self.top_checkbox.isChecked(),
            "auto_submit": self.submit_checkbox.isChecked(),
            "window_width": self.width_edit.text(),
            "window_height": self.height_edit.text()
        }





